package com.weitu.permission.authority.service.impl;


import com.weitu.base.entity.Tree;
import com.weitu.permission.authority.entity.Authority;
import com.weitu.permission.authority.entity.AuthorityGroup;
import com.weitu.permission.authority.mapper.AuthorityGroupMapper;
import com.weitu.permission.authority.mapper.AuthorityMapper;
import com.weitu.permission.authority.service.AuthorityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class AuthorityServiceImpl implements AuthorityService {

    private final AuthorityMapper authorityMapper;
    private final AuthorityGroupMapper authorityGroupMapper;

    @Autowired
    public AuthorityServiceImpl(AuthorityMapper authorityMapper,AuthorityGroupMapper authorityGroupMapper) {
        this.authorityMapper = authorityMapper;
        this.authorityGroupMapper = authorityGroupMapper;

    }

    @Override
    public Authority selectAuthorityById(String id) {
        return authorityMapper.selectAuthorityById(id);
    }

    @Override
    public Authority selectByCode(String Code) {
        return authorityMapper.selectByCode(Code);
    }

    @Override
    public int deleteById(String id) {
        return authorityMapper.deleteById(id);
    }

    @Override
    public int insert(Authority authority) {
        return authorityMapper.insert(authority);
    }

    @Override
    public int insertSelective(Authority authority) {
        return authorityMapper.insertSelective(authority);
    }

    @Override
    public int updateByIdSelective(Authority authority) {
        return authorityMapper.updateByIdSelective(authority);
    }

    @Override
    public List<Authority>  queryAuthorityList(Map map) {
        return authorityMapper.queryAuthorityList(map);
    }

    @Override
    public List<Authority>selectAuthorityByGroupId(String GroupId){
        return authorityMapper.selectAuthorityByGroupId(GroupId);
    };
    @Override
    public int queryTotalCount(Map map) {
        return authorityMapper.queryTotalCount(map);
    }

    @Override
    public List<Tree> getAuthorityTree(Map map){
        List<Tree> panentTreeList = new ArrayList<Tree>();
        List<Tree> childrenTreeList;
        List<AuthorityGroup>  AuthorityGroupList = this.authorityGroupMapper.getAuthorityGroupList();
        Tree authorityGroupTree;
        Tree authorityTree;

        for (AuthorityGroup authorityGroup : AuthorityGroupList) {
            String authorityGroupId = authorityGroup.getId();
            String authorityGroupName =authorityGroup.getName();
            List<Authority> authorityList =  authorityMapper.selectAuthorityByGroupId(authorityGroupId);
            if(authorityList.size()>0){
                map.put("groupId",authorityGroupId );
                List<Authority>AuthorityeCheckedList=authorityMapper.queryAuthorityCheckedList(map);
                authorityGroupTree = new Tree();
                authorityGroupTree.setId(authorityGroupId);
                authorityGroupTree.setText(authorityGroupName);
                childrenTreeList = new ArrayList<Tree>();
                for (Authority authority : authorityList) {
                    String authorityId = authority.getId();
                    String authorityName =authority.getName();
                    authorityTree = new Tree();
                    authorityTree.setId(authorityId);
                    authorityTree.setText(authorityName);
                    authorityTree.setChecked(null);
                    for(Authority AuthorityeChecked:AuthorityeCheckedList){
                        if(authorityId.equals(AuthorityeChecked.getId())){
                            authorityTree.setChecked("true");
                        }
                    }
                    childrenTreeList.add(authorityTree);
                }
                authorityGroupTree.setChildren(childrenTreeList);
                panentTreeList.add(authorityGroupTree);
            }
        }

        return panentTreeList;
    }


}
